var token=$.cookie('token');
$(document).ready(function(){
	initLoad();
	$('#exportExcel').click(function(){
		window.location.href = "admin/question/exportExcel?questionType="+$('#questionType').val()+"&questionLevel="+$('#questionLevel').val()+"&isDel="+$('#isDel').val()+"&questionContentStr="+$('#questionContentStr').val()+'&dateBegin='+$('#dateBegin').val()+'&dateEnd='+$('#dateEnd').val()+'&token='+token;
	});
	//全选和反选操作
    $("#questionChHeader").click(function(){
		if(this.checked){
			$("input[type='checkbox'][name='idcheck']").each(function(e){
				this.checked = true;
			});
	     }else {
				$("input[type='checkbox'][name='idcheck']").each(function(e){
							this.checked = false;
				});
		 }
    });
    $('#questionDelete').click(function(){
    	var cks = $("input[name='idcheck']");
		var ids="";
		$.each(cks,function(i,n){
		  if($(this).is(':checked')){
			  if(ids==""){
				  ids+=$(this).val(); 
			  }else{
				  ids+=","+$(this).val(); 
			  }
          }
		});
		$.yxpConfirmMsg('确认要删除该试题吗?',function(){
			$.ajax({
				type:'get',
				url:"admin/question/deleteByIds",
				data:{'ids':ids},
				dataType: "json",
				success:function(data){
					if(data.msgCode==1000){
						$.yxpAlertMsg('删除成功');
						initLoad();
					}
				},
				error:function(data){
					$.yxpAlertMsg('删除失败');
				}
			});}
		);
    });
});
initLoad=function(){
	$.ajax({
		type:'get',
		url:"admin/question/questionPage",
		data:{'pageNo':1,'pageSize':10,'token':token},
		dataType: "json",
   	    success:function(data){
   	    	console.log(data.list);
   	    	if(data.list!=null && data.list.length>0){
   	    		$('#question_tbody').empty();
   	    		$.each(data.list,function(i,n){
   	    			console.log(n.explanation);
   	    			var hint = '<td></td>';
   	    			var explanation = '<td></td>';
   	    			if(n.hint!=null){
   	    				hint='<td title="'+n.hint+'">'+(n.hint.length<=12?n.hint:n.hint.substring(0,12)+'...')+'</td>';
   	    			}
   	    			if(n.explanation!=null){
   	    				explanation='<td title="'+n.explanation+'">'+(n.explanation.length<=12?n.explanation:n.explanation.substring(0,12)+'...')+'</td>';
   	    			}
   	    			$('#question_tbody').append('<tr>'+
                            '<td><input type="checkbox" name="idcheck" value="'+n.id+'"/>'+n.id+'</td>'+
                            '<td>'+(n.questionType==0?'看图猜成语':'单选题')+'</td>'+
                            '<td>'+n.questionLevel+'</td>'+
                            '<td>'+n.questionContent+'</td>'+
                            '<td>'+(n.questionOptions==null?"":n.questionOptions)+'</td>'+
                            '<td>'+n.questionAnswer+'</td>'+
                            hint+explanation+
                            '<td>'+(n.answerTimes==null?'':n.answerTimes)+'</td>'+
                            '<td>'+(n.answerErrorTimes==null?'':n.answerErrorTimes)+'</td>'+
                            '<td>'+n.createtimeStr+'</td>'+
                            '<td>'+n.updatetimeStr+'</td>'+
                            '<td>'+(n.isDel==0?'未删除':'已删除')+'</td>'+
                            '<td><a href="#" onclick="javascript:delQuestionById(this);">删除</a>&nbsp;<a href="#" onclick="javascript:recoverQuesById(this);">恢复</a></td>'+
                        '</tr>');
   	    		});
   	    		$('#all').text(data.pages);
   	    		$('#now').text(data.pageNum);
   	    	}else{
   	    		window.location.href = data.orderBy;
   	    	}
   	    }
	});
}
recoverQuesById=function(obj){
	$.ajax({
		type:'get',
		url:"admin/question/recoverQuesById",
		data:{'id':parseInt($(obj).parent().parent().children(':first').text())},
		dataType: "json",
		success:function(data){
			if(data.msgCode===1000){
				$.yxpAlertMsg('恢复成功!');
				initLoad();
			}
		}
	});
}
delQuestionById=function(obj){
	$.yxpConfirmMsg('确认要删除该试题吗?',function(){
			$.ajax({
				type:'get',
				url:"admin/question/delQuestionById",
				data:{'id':parseInt($(obj).parent().parent().children(':first').text())},
				dataType: "json",
				success:function(data){
					if(data.msgCode===1000){
						$.yxpAlertMsg('删除成功!');
						initLoad();
					}
				}
			});
		}
	);
}
queryResultList=function(){
	$.ajax({
		type:'get',
		url:"admin/question/questionPage",
		data:{'pageNo':1,'pageSize':10,'questionType':$('#questionType').val(),'questionLevel':$('#questionLevel').val(),'isDel':$('#isDel').val(),'questionContentStr':$('#questionContentStr').val(),'dateBegin':$('#dateBegin').val(),'dateEnd':$('#dateEnd').val(),'token':token},
		dataType:'json',
		success:function(data){
   	    	if(data.list!=null){
   	    		$('#question_tbody').empty();
   	    		$.each(data.list,function(i,n){
   	    			console.log(n.questionContent);
   	    			var hint = '<td></td>';
   	    			var explanation = '<td></td>';
   	    			if(n.hint!=null){
   	    				hint='<td title="'+n.hint+'">'+(n.hint.length<=12?n.hint:n.hint.substring(0,12)+'...')+'</td>';
   	    			}
   	    			if(n.explanation!=null){
   	    				explanation='<td title="'+n.explanation+'">'+(n.explanation.length<=12?n.explanation:n.explanation.substring(0,12)+'...')+'</td>';
   	    			}
   	    			$('#question_tbody').append('<tr>'+
                            '<td><input type="checkbox" name="idcheck" value="'+n.id+'"/>'+n.id+'</td>'+
                            '<td>'+(n.questionType==0?'看图猜成语':'单选题')+'</td>'+
                            '<td>'+n.questionLevel+'</td>'+
                            '<td>'+n.questionContent+'</td>'+
                            '<td>'+(n.questionOptions==null?"":n.questionOptions)+'</td>'+
                            '<td>'+n.questionAnswer+'</td>'+
                            hint+explanation+
                            '<td>'+(n.answerTimes==null?'':n.answerTimes)+'</td>'+
                            '<td>'+(n.answerErrorTimes==null?'':n.answerErrorTimes)+'</td>'+
                            '<td>'+n.createtimeStr+'</td>'+
                            '<td>'+n.updatetimeStr+'</td>'+
                            '<td>'+(n.isDel==0?'未删除':'已删除')+'</td>'+
                            '<td><a href="#" onclick="javascript:delQuestionById(this);">删除</a>&nbsp;<a href="#" onclick="javascript:recoverQuesById(this);">恢复</a></td>'+
                        '</tr>')
   	    		});
   	    		$('#all').text(data.pages);
   	    		$('#now').text(data.pageNum);
   	    	}
   	    }
	});
}
prevPage=function(){
	if($('#now').text()==1){
		$.yxpAlertMsg('已经是第一页');
		return;
	}
	var pageNo=parseInt($('#now').text())-1;
	$('#now').text(pageNo);
	$.ajax({
		type:'get',
		url:"admin/question/questionPage",
		data:{'pageNo':pageNo,'pageSize':10,'questionType':$('#questionType').val(),'questionLevel':$('#questionLevel').val(),'isDel':$('#isDel').val(),'questionContentStr':$('#questionContentStr').val(),'dateBegin':$('#dateBegin').val(),'dateEnd':$('#dateEnd').val(),'token':token},
		dataType:'json',
		success:function(data){
   	    	if(data.list!=null){
   	    		$('#question_tbody').empty();
   	    		$.each(data.list,function(i,n){
   	    			var hint = '<td></td>';
   	    			var explanation = '<td></td>';
   	    			if(n.hint!=null){
   	    				hint='<td title="'+n.hint+'">'+(n.hint.length<=12?n.hint:n.hint.substring(0,12)+'...')+'</td>';
   	    			}
   	    			if(n.explanation!=null){
   	    				explanation='<td title="'+n.explanation+'">'+(n.explanation.length<=12?n.explanation:n.explanation.substring(0,12)+'...')+'</td>';
   	    			}
   	    			$('#question_tbody').append('<tr>'+
                            '<td><input type="checkbox" name="idcheck" value="'+n.id+'"/>'+n.id+'</td>'+
                            '<td>'+(n.questionType==0?'看图猜成语':'单选题')+'</td>'+
                            '<td>'+n.questionLevel+'</td>'+
                            '<td>'+n.questionContent+'</td>'+
                            '<td>'+(n.questionOptions==null?"":n.questionOptions)+'</td>'+
                            '<td>'+n.questionAnswer+'</td>'+
                            hint+explanation+
                            '<td>'+(n.answerTimes==null?'':n.answerTimes)+'</td>'+
                            '<td>'+(n.answerErrorTimes==null?'':n.answerErrorTimes)+'</td>'+
                            '<td>'+n.createtimeStr+'</td>'+
                            '<td>'+n.updatetimeStr+'</td>'+
                            '<td>'+(n.isDel==0?'未删除':'已删除')+'</td>'+
                            '<td><a href="#" onclick="javascript:delQuestionById(this);">删除</a>&nbsp;<a href="#" onclick="javascript:recoverQuesById(this);">恢复</a></td>'+
                        '</tr>');
   	    		});
   	    	}
   	    }
	});
}
nextPage=function(){
	if($('#now').text()==$('#all').text()){
		$.yxpAlertMsg('已经是最后一页');
		return;
	}
	var pageNo=parseInt($('#now').text())+1;
	$('#now').text(pageNo);
	$.ajax({
		type:'get',
		url:"admin/question/questionPage",
		data:{'pageNo':pageNo,'pageSize':10,'questionType':$('#questionType').val(),'questionLevel':$('#questionLevel').val(),'isDel':$('#isDel').val(),'questionContentStr':$('#questionContentStr').val(),'dateBegin':$('#dateBegin').val(),'dateEnd':$('#dateEnd').val(),'token':token},
		dataType:'json',
		success:function(data){
   	    	if(data.list!=null){
   	    		$('#question_tbody').empty();
   	    		$.each(data.list,function(i,n){
   	    			var hint = '<td></td>';
   	    			var explanation = '<td></td>';
   	    			if(n.hint!=null){
   	    				hint='<td title="'+n.hint+'">'+(n.hint.length<=12?n.hint:n.hint.substring(0,12)+'...')+'</td>';
   	    			}
   	    			if(n.explanation!=null){
   	    				explanation='<td title="'+n.explanation+'">'+(n.explanation.length<=12?n.explanation:n.explanation.substring(0,12)+'...')+'</td>';
   	    			}
   	    			$('#question_tbody').append('<tr>'+
                            '<td><input type="checkbox" name="idcheck" value="'+n.id+'"/>'+n.id+'</td>'+
                            '<td>'+(n.questionType==0?'看图猜成语':'单选题')+'</td>'+
                            '<td>'+n.questionLevel+'</td>'+
                            '<td>'+n.questionContent+'</td>'+
                            '<td>'+(n.questionOptions==null?"":n.questionOptions)+'</td>'+
                            '<td>'+n.questionAnswer+'</td>'+
                            hint+explanation+
                            '<td>'+(n.answerTimes==null?'':n.answerTimes)+'</td>'+
                            '<td>'+(n.answerErrorTimes==null?'':n.answerErrorTimes)+'</td>'+
                            '<td>'+n.createtimeStr+'</td>'+
                            '<td>'+n.updatetimeStr+'</td>'+
                            '<td>'+(n.isDel==0?'未删除':'已删除')+'</td>'+
                            '<td><a href="#" onclick="javascript:delQuestionById(this);">删除</a>&nbsp;<a href="#" onclick="javascript:recoverQuesById(this);">恢复</a></td>'+
                        '</tr>');
   	    		});
   	    	}
   	    }
	});
}
turnPage=function(){
	var pageNo = $('#turn').val();
	if(parseInt(pageNo)>parseInt($('#all').text())){
		return;
	}
	$.ajax({
		type:'get',
		url:"admin/question/questionPage",
		data:{'pageNo':pageNo,'pageSize':10,'questionType':$('#questionType').val(),'questionLevel':$('#questionLevel').val(),'isDel':$('#isDel').val(),'questionContentStr':$('#questionContentStr').val(),'dateBegin':$('#dateBegin').val(),'dateEnd':$('#dateEnd').val(),'token':token},
		dataType:'json',
		success:function(data){
   	    	if(data.list!=null){
   	    		$('#now').text(pageNo);
   	    		$('#question_tbody').empty();
   	    		$.each(data.list,function(i,n){
   	    			var hint = '<td></td>';
   	    			var explanation = '<td></td>';
   	    			if(n.hint!=null){
   	    				hint='<td title="'+n.hint+'">'+(n.hint.length<=12?n.hint:n.hint.substring(0,12)+'...')+'</td>';
   	    			}
   	    			if(n.explanation!=null){
   	    				explanation='<td title="'+n.explanation+'">'+(n.explanation.length<=12?n.explanation:n.explanation.substring(0,12)+'...')+'</td>';
   	    			}
   	    			$('#question_tbody').append('<tr>'+
                            '<td><input type="checkbox" name="idcheck" value="'+n.id+'"/>'+n.id+'</td>'+
                            '<td>'+(n.questionType==0?'看图猜成语':'单选题')+'</td>'+
                            '<td>'+n.questionLevel+'</td>'+
                            '<td>'+n.questionContent+'</td>'+
                            '<td>'+(n.questionOptions==null?"":n.questionOptions)+'</td>'+
                            '<td>'+n.questionAnswer+'</td>'+
                            hint+explanation+
                            '<td>'+(n.answerTimes==null?'':n.answerTimes)+'</td>'+
                            '<td>'+(n.answerErrorTimes==null?'':n.answerErrorTimes)+'</td>'+
                            '<td>'+n.createtimeStr+'</td>'+
                            '<td>'+n.updatetimeStr+'</td>'+
                            '<td>'+(n.isDel==0?'未删除':'已删除')+'</td>'+
                            '<td><a href="#" onclick="javascript:delQuestionById(this);">删除</a>&nbsp;<a href="#" onclick="javascript:recoverQuesById(this);">恢复</a></td>'+
                        '</tr>');
   	    		});
   	    	}
   	    }
	});
}
setTarget=function(obj){
	if(token){
		$(obj).attr('href','excel/question_template.xlsx');
	}else{
		$(obj).attr('href','');
	}
}
